package com.skyworthclub.competition.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.skyworthclub.competition.beans.Match;
import com.skyworthclub.competition.mapper.MatchMapper;
import com.skyworthclub.competition.service.AdminService;
import com.skyworthclub.competition.service.MatchElasticsearchService;
import com.skyworthclub.competition.service.MatchService;
import com.skyworthclub.competition.utils.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author li
 * @version 1.0
 * @since 18-10-26 下午7:10
 * 开启异步任务 @EnableAsync
 **/
@EnableAsync
@Service
public class AdminServiceImpl implements AdminService {

    private final MatchMapper matchMapper;

    private final EmailService emailService;

    private final MatchService matchService;

    private final MatchElasticsearchService matchElasticsearchService;

    private static final int PAGE_SIZE = 5;

    @Autowired
    public AdminServiceImpl(MatchMapper matchMapper,
                            EmailService emailService,
                            MatchService matchService,
                            MatchElasticsearchService matchElasticsearchService) {
        this.matchMapper = matchMapper;
        this.emailService = emailService;
        this.matchService = matchService;
        this.matchElasticsearchService = matchElasticsearchService;
    }


    @Override
    public Page<Match> checkMatch(Integer pageNum) {
        Page<Match> page = new Page<>(pageNum, PAGE_SIZE);
        //设定升序排列字段
        List<String> asc = new ArrayList<>();
        asc.add("is_checked");

        //设定降序排列字段
        List<String> desc = new ArrayList<>();
        desc.add("logic_flag");
        page.setDescs(desc);

        page.setAscs(asc);
        List<Match> matchList = matchMapper.checkPage(page);
        page.setRecords(matchList);
        return page;
    }


    @Override
    public boolean passChecking(Integer matchId, String publisher, String matchName) {
        Match match = new Match();
        //更改状态
        match.setIsChecked((short) 1);
        EntityWrapper<Match> wrapper = new EntityWrapper<>();
        wrapper.eq("match_id", matchId)
                .and()
                .eq("match_publisher", publisher);
        boolean update = matchService.updateCheckedStatus(match, wrapper);
        if (update) {
            //邮件提醒
            emailService.matchPublishPassNotice(publisher, matchName);
            //同步到es索引中
            Match one = matchService.selectOne(matchId);
            matchElasticsearchService.save(one);
        }
        return update;

    }


    @Override
    public boolean unPassChecking(Integer matchId, String publisher, String matchName, String message) {
        Match match = new Match();
        //更改状态
        match.setIsChecked((short) -1);
        EntityWrapper<Match> wrapper = new EntityWrapper<>();
        wrapper.eq("match_id", matchId)
                .and()
                .eq("match_publisher", publisher);
        boolean update = matchService.update(match, wrapper);
        if (update) {
            //邮件提醒
            emailService.matchPublishUnPassNotice(publisher,matchName, message);
        }
        return update;
    }

}
